package com.hsy.linemoney.requests;

import android.support.v4.util.SparseArrayCompat;

import com.hsy.linemoney.apiservice.Callback;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * Created by MartinKent on 2016/9/9.
 */
public class RequestQueue<T> {

    private SparseArrayCompat<RequestTask> requests = new SparseArrayCompat<>();
    private OnRequestExecutedListener mOnRequestExecutedListener;

    public void removeTask(int what) {
        synchronized (RequestTask.class) {
            RequestTask task = requests.get(what, null);
            if (null != task) {
                task.cancel(true);
                requests.remove(what);
            }
        }
    }

    public void clearTask() {
        synchronized (RequestTask.class) {
            while (requests.size() > 0) {
                requests.valueAt(0).cancel(true);
                requests.removeAt(0);
            }
        }
    }

    public RequestQueue addTask(final int what, final String api, final JSONObject params, final Callback<T> callback, boolean... checkStatusCode) {
        synchronized (RequestTask.class) {
            requests.put(what, new RequestTask<>(what, api, params, new Callback<T>() {
                @Override
                public void onSuccess(int what, T result) {
                    if (null != callback) {
                        callback.onSuccess(what, result);
                    }
                    if (null != mOnRequestExecutedListener) {
                        mOnRequestExecutedListener.onRequestExecuted(what);
                    }
                    requests.remove(what);
                    if (0 == requests.size()) {
                        if (null != mOnRequestExecutedListener) {
                            mOnRequestExecutedListener.onAllRequestExecuted();
                        }
                    }
                }

                @Override
                public void onError(int what, int type, String msg, Exception e) {
                    if (null != callback) {
                        callback.onError(what, type, msg, e);
                    }
                    if (null != mOnRequestExecutedListener) {
                        mOnRequestExecutedListener.onRequestExecuted(what);
                    }
                    requests.remove(what);
                    if (0 == requests.size()) {
                        if (null != mOnRequestExecutedListener) {
                            mOnRequestExecutedListener.onAllRequestExecuted();
                        }
                    }
                }
            }, checkStatusCode));
        }
        return this;
    }

    public void start() {
        start(null);
    }

    public void start(OnRequestExecutedListener listener) {
        synchronized (RequestTask.class) {
            mOnRequestExecutedListener = listener;
            for (int i = 0; i < requests.size(); i++) {
                requests.valueAt(i).invoke();
            }
        }
    }

    public interface OnRequestExecutedListener {
        void onRequestExecuted(int what);

        void onAllRequestExecuted();
    }
}
